$title	Read the TERM Database File, Filter out Small Numbers and Save

SET	user(*)	Industries and components of final demand
	c(*)	Commodities
	l(*)	Skills
	i(user)	Industries
	r(*)	Regions of use - production - origin
	s(*)	States;

$call  'har2gdx aggmdb'
$gdxin 'aggmdb.gdx'
$load user c=com i=ind l=occ r=regdst s=state

ALIAS (r,rr,rx,rm);

SET	fd(user)	Components of final demand /HOU, INV,GOV, EXP/,
	mrg(c)		Margins /trade, transport/,
	wind(i)		Water-using industries
	src		Sources /dom, imp/;

PARAMETER
	cap(i,r)		"Rentals to capital (48x20)",
	lab(i,l,r)		"Wage matrix (48x9x20)",
	lnd(i,r)		"Rentals to land (48x20)",
	ptx(i,r)		"Taxes on production (48x20)",
	invest(c,i,r)		"investment at purchasers prices (48x48x20)",
	use(c,src,user,r)	"Basic + margin flows (48x2x52x20)",
	dep(i)			"Depreciation rates adapted from MONASH model (48)",
	expelas(c)		"Export demand elasticity (48) (+)",
	feed0(i)		"FEED0(i (48))",
	make(c,i,r)		"MAKE matrix (48x48x20)",
        suppmar(c,r,r,r)	"Margins supplied by REGPRD on goods passing from (2x20x20x20)",

	armsigma(c)		"Armington from PDA (Jan 2002),remapped (48)",
	frisch(r)		"Frisch LES parameter= - (total/luxury (20))",
	sigma1prim(i)		"CES substitution, primary factors (48)",
	pop(r)			"Regional population June 2001 (20)",
	regstate(r,s)		"Maps regions to states (20x8)",
	sigmadomdom(c)		"Between-region Armington (48)",
	sigma1lab(i)		"CES substitution between skill types (48)",
	sigmamar(mrg)		"Elasticity of substitution between regions of margin production (2)",

	stocks(i,r)		"Domestic inventories (48x20)",

	trade(c,src,r,r)	"Sourcing matrix (48x2x20x20)",
	tradmar(c,src,mrg,r,r)	"Margins on trade matrix (48x2x2x20x20)",
	taxes(c,src,user,r)	"Taxes  (48x2x52x20))",

	usewater(i,r)		"Value of water used (48x20)",
	wateruse(i,r)		"Water used (16x12)",
	wat_elas(i)		"Water-saving responsiveness (48)",
	wrights(i,r)		"Water rights (16x12)";

$load cap=v1cap lab=v1lab lnd=v1lnd ptx=v1ptx invest use=basmar dep expelas feed0 make suppmar armsigma 
$load frisch sigma1prim pop regstate sigmadomdom sigma1lab sigmamar stocks 
$load tradmar trade taxes usewater wateruse=waterirg wat_elas wrights=waterirga wind
$load 

*	Delete the file after the data have been read:

$call 'rm aggmdb.gdx'

parameter	gdplevel(i,*);
gdplevel(i,"value") = sum(r, cap(i,r) + lnd(i,r) + sum(l, lab(i,l,r)) + ptx(i,r));
alias (i,j);
gdplevel(i,"pct") = 100 * sum(r, cap(i,r) + lnd(i,r) + sum(l, lab(i,l,r)) + ptx(i,r))
		/   sum((j,r), cap(j,r) + lnd(j,r) + sum(l, lab(j,l,r)) + ptx(j,r))
display gdplevel;

cap(i,r)$(not round(cap(i,r),6)) = 0;
lab(i,l,r)$(not round(lab(i,l,r),6)) = 0;
lnd(i,r)$(not round(lnd(i,r),6)) = 0;
ptx(i,r)$(not round(ptx(i,r),6)) = 0;
invest(c,i,r)$(not round(invest(c,i,r),6)) = 0;
use(c,src,user,r)$(not round(use(c,src,user,r),6)) = 0;
dep(i)$(not round(dep(i),6)) = 0;
expelas(c)$(not round(expelas(c),6)) = 0;
feed0(i)$(not round(feed0(i),6)) = 0;
make(c,i,r)$(not round(make(c,i,r),6)) = 0;
suppmar(c,rx,rm,r)$(not round(suppmar(c,rx,rm,r),6)) = 0;

armsigma(c)$(not round(armsigma(c),6)) = 0;
frisch(r)$(not round(frisch(r),6)) = 0;
sigma1prim(i)$(not round(sigma1prim(i),6)) = 0;
pop(r)$(not round(pop(r),6)) = 0;
regstate(r,s)$(not round(regstate(r,s),6)) = 0;
sigmadomdom(c)$(not round(sigmadomdom(c),6)) = 0;
sigma1lab(i)$(not round(sigma1lab(i),6)) = 0;
sigmamar(mrg)$(not round(sigmamar(mrg),6)) = 0;

stocks(i,r)$(not round(stocks(i,r),6)) = 0;

trade(c,src,rm,r)$(not round(trade(c,src,rm,r),6)) = 0;
tradmar(c,src,mrg,rm,r)$(not round(tradmar(c,src,mrg,rm,r),6)) = 0;
taxes(c,src,user,r)$(not round(taxes(c,src,user,r),6)) = 0;
taxes(c,src,user,r)$(not use(c,src,user,r)) = 0;

usewater(i,r)$(not round(usewater(i,r),6)) = 0;
wateruse(i,r)$(not round(wateruse(i,r),6)) = 0;
wat_elas(i)$(not round(wat_elas(i),6)) = 0;
wrights(i,r)$(not round(wrights(i,r),6)) = 0;

parameter	vtot(i,r)	Total output;
vtot(i,r) = sum((c,src), use(c,src,i,r) + taxes(c,src,i,r)) 
		+ usewater(i,r)
		+ sum(l,lab(i,l,r)) 
		+ cap(i,r) 
		+ lnd(i,r) 
		+ ptx(i,r);

parameter	imbalance(i,r)	Zero profit imbalances (% deviation);
imbalance(i,r)$round(vtot(i,r),5) = round(100 * (vtot(i,r)-sum(c, make(c,i,r))-stocks(i,r))/vtot(i,r), 2);
display imbalance;

parameter	demand(c,src,r)		Demand,
		supply(c,src,r)		Supply
		balance(c,src,r)	Supply-demand balance;

demand(c,src,r) = sum(user, use(c,src,user,r));
supply(c,src,r) = sum(rr, trade(c,src,rr,r) + sum(mrg, tradmar(c,src,mrg,rr,r)));
balance(c,src,r)$supply(c,src,r) = round(100 * (demand(c,src,r)/supply(c,src,r)-1),2);
display balance;
parameter trace(*);
trace("use") = sum((c,src,i,r), use(c,src,i,r));
trace("taxes") = sum((c,src,i,r) , taxes(c,src,i,r));
trace("usewater") = sum((i,r), usewater(i,r));
trace("sum") = sum((i,l,r), lab(i,l,r));
trace("cap") = sum((i,r) , cap(i,r) );
trace("lnd") = sum((i,r) , lnd(i,r) );
trace("ptx") = sum((i,r), ptx(i,r));
trace("make") = sum((c,i,r), make(c,i,r));
trace("stocks") = sum((i,r), stocks(i,r));
trace("trade") = sum((c,src,rr,r), trade(c,src,rr,r));
trace("tradmar") = sum((c,src,mrg,rr,r), tradmar(c,src,mrg,rr,r));
display trace;
$exit



parameter	marbal(mrg,rx,rm)	Margin balance;

marbal(mrg,rx,rm) = round(sum(r, suppmar(mrg,rx,rm,r)) - sum((c,src), tradmar(c,src,mrg,rx,rm)),3);
display marbal;

balance(c,"dom",r) = round(sum(i, make(c,i,r)) - sum((rx,rm), suppmar(c,rx,rm,r)) - sum(rr, trade(c,"dom",r,rr)),3);
display balance;

*.parameter	impsupply(c,r)		Imported goods balance;
*.impsupply(c,r) = sum(rr, trade(c,"imp",r,rr));
*.display impsupply;

parameter watervalue;
watervalue(i) = sum(r, usewater(i,r));
display watervalue;
$exit


parameter	gdpincsum	Summary of GDP by income category,
		gdpexpsum	Summary of GDP by expenditure category;

set gdpinccat /Land,Labour,Capital,ProdTax,ComTax,Water,Total/;

gdpincsum(r,"Land")    = sum(i, lnd(i,r));
gdpincsum(r,"Labour")  = sum((i,l), lab(i,l,r));
gdpincsum(r,"Capital") = sum(i, cap(i,r));
gdpincsum(r,"ProdTax") = sum(i, ptx(i,r));
gdpincsum(r,"ComTax")  = sum((c,src,i), taxes(c,src,i,r));
gdpincsum(r,"Water")   = sum(i, usewater(i,r));
gdpincsum(r,"total")   = sum(gdpinccat,gdpincsum(r,gdpinccat));

set gdpexpcat	Expenditure categories of GDP
	/hou, inv, gov, exp, stocks, Imports, RExports, RImports, NetMar/;

gdpexpsum(r,fd) = sum((c,src), use(c,src,fd,r) + taxes(c,src,fd,r)); 
gdpexpsum(r,"Stocks")   = sum(i, stocks(i,r));
gdpexpsum(r,"Imports")  = -sum((c,rr), trade(c,"imp",r,rr));
gdpexpsum(r,"RExports") = sum((c,src,rr), trade(c,src,r,rr)) - sum((c,src),trade(c,src,r,r));
gdpexpsum(r,"RImports") = sum((c,src,rr), trade(c,src,rr,r)) - sum((c,src),trade(c,src,r,r));
gdpexpsum(r,"NetMar") = sum((mrg,rx,rm), suppmar(mrg,rx,rm,r)) - sum((mrg,rx,rr), suppmar(mrg,rx,r,rr));
gdpexpsum(r,"total") = sum(gdpexpcat,gdpexpsum(r,gdpexpcat));

display gdpincsum, "This is invalid:", gdpexpsum;


parameter	c0(r)			Aggregate final consumption
		ta(c,src,user,r)	Tax rate on aggregate goods
		pa0(c,src,user,r)	Reference price
		i0(r)			Base year investment
		ty(i,r)			Industry tax
		g0			Government demand
		supply(c,src,r)		Aggregate supply
		demdmar(mrg,rx,rm)	Margins demand
		bopdef			Current account deficit
		trnsf(r)		Net transfers to region r
		e0(c,r)			Exports
		vx0			Value of exports;

ta(c,src,user,r)$taxes(c,src,user,r) = taxes(c,src,user,r)/use(c,src,user,r);
pa0(c,src,user,r) = 1 + ta(c,src,user,r);
c0(r) = sum((c,src), use(c,src,"hou",r)+taxes(c,src,"hou",r));
i0(r) = sum((c,src), use(c,src,"inv",r)+taxes(c,src,"inv",r));
g0 = sum((c,src,r),use(c,src,"gov",r)+taxes(c,src,"gov",r));
supply(c,src,r) = sum(rr, trade(c,src,rr,r)+sum(mrg,tradmar(c,src,mrg,rr,r)));
demdmar(mrg,rx,rm) = sum(r, suppmar(mrg,rx,rm,r));
ty(i,r)$ptx(i,r) = ptx(i,r) / vtot(i,r);
trnsf(r) = c0(r) + sum(i, stocks(i,r)) + i0(r)
		- sum(i, sum(l, lab(i,l,r)) + lnd(i,r) + cap(i,r));

e0(c,r) = use(c,"dom","exp",r);
vx0 = sum((c,r), e0(c,r));

set		dom(src)/dom/, imp(src)/imp/;

bopdef = sum((c,rx,rm), trade(c,"imp",rx,rm)) - vx0;

display r, i, c;

execute_unload 'term.gdx',r,i,src,mrg,use,taxes,trade,tradmar,suppmar,usewater,cap,lnd,lab,make,stocks,
			expelas,sigma1prim,sigma1lab,sigmamar;
